// pages/oilForm/oilForm.js
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    station:null,
    nowOilType:null,
    oils:[],
  
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      'station_id': options.stationid
    })
    this.getInfo();
  
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },


  getInfo: function () {
    let _this = this;
    wx.cloud.init()
    wx.cloud.callFunction({
      name: 'getStationInfo',
      data: {
        station_id: this.data.station_id
      },
    }).then(res => {
      console.log('callFunction test result: ', res)
      _this.setData({
        station: res.result,
        nowOilType: res.result.oil_price.oils[0],
        oils: res.result.oil_price.oils
      });

    })
  },

  oilTypeChange(event) {
    console.log(this.data.station)
    this.setData({
      nowOilType: this.data.station.oil_price.oils[event.currentTarget.dataset.key]
    })
  },
  savePrice: function () {
    let _this = this;
    for (let item of this.data.oils){
      if (item.oil_type == _this.data.nowOilType.oil_type){
        item.price = _this.data.nowOilType.price
      }
    }
    console.log()
    wx.cloud.init()
    wx.cloud.callFunction({
      name: 'setStationPrice',
      data: {
        station_id: this.data.station._id,
        oil_id: this.data.station.oil_price._id,
        oil_prices: _this.data.oils        
      },
    }).then(res => {
      wx.showToast({
        title: '爆料成功',
        icon: 'success',
        duration: 2000
      })
      console.log('修改成功: ', res)

    })
  },

  resetPrice: function(e){
    var value = e.detail.value
    var price = 'nowOilType.price'
    this.setData({
      [price]: value
    })
  }


})